package web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mysql.jdbc.Connection;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Connection connection;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	/*public void init() throws ServletException {
		try {
			// "com.mysql.jdbc.Driver"
			Class.forName("com.mysql.jdbc.Driver");
			String dbURL = "jdbc:mysql://localhost/shoponline";
			String username = "root";
			String password = "12345";
			connection = (Connection) DriverManager.getConnection(dbURL,
					username, password);
		} catch (ClassNotFoundException e) {
			System.out.println("Database driver not found.");
		} catch (SQLException e) {
			System.out.println("Error opening the db connection: "
					+ e.getMessage());
		}
	}*/

	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
		/*try {
			if (checkUser(user, pass) == true) {
				out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 ");
				out.println("Transitional//EN\">\n");
				out.println("<html><head><title>Wellcome !</title></head><body>");
				out.println("Wellcome " + user + " to J2EE");
				out.print("</body></hmtl>");
			} else {
				out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 ");
				out.println("Transitional//EN\">\n");
				out.println("<html><head><title>Access Denied!</title></head><body>");
				out.println("Your user or password is not match ,please <a href='register.jsp'>Register</a>");
				out.print("</body></hmtl>");
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// } catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		// System.out.print(e.getMessage());
		// }
		out.close();*/
	}

	/*public boolean checkUser(String user, String password)
			throws ClassNotFoundException, ServletException {
		boolean check = false;
		init();
		try {
			if (connection != null) {
				String sql = "SELECT * FROM taikhoan WHERE userName='" + user
						+ "' AND password='" + password + "'";
				Statement st = (Statement) connection.createStatement(
						ResultSet.TYPE_SCROLL_INSENSITIVE,
						ResultSet.CONCUR_UPDATABLE);
				ResultSet rs = st.executeQuery(sql);
				if (rs.next())
					check = true;
				else
					check = false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return check;
	}*/

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		//
		//connection=null;
		String n=request.getParameter("username");  //<lay ten thuoc tinh name="username" trong textbox cua  tag input>
		String p=request.getParameter("password"); 
		 HttpSession session = request.getSession(false);  
	        if(session!=null)  
	        session.setAttribute("name", n);  
	  
	        if(LoginDao.validate(n, p)){    
	            RequestDispatcher rd=request.getRequestDispatcher("index.jsp");    
	            rd.forward(request,response);    
	        }    
	        else{    
	            out.print("<p style=\"color:red\">Sorry username or password error</p>");    
	            RequestDispatcher rd=request.getRequestDispatcher("dangnhap.jsp");    
	            rd.include(request,response);    
	        }    
	  
	        out.close();    
	}

}
